package com.shenzhen.horselive.ui.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.shenzhen.horselive.R;
import com.shenzhen.horselive.User;
import com.shenzhen.horselive.adapter.RedDetailsAdapter;
import com.shenzhen.horselive.api.ApiFactory;
import com.shenzhen.horselive.base.AppBarActivity;
import com.shenzhen.horselive.beans.RedDetailsAboutBean;
import com.shenzhen.horselive.beans.RedDetailsBean;
import com.shenzhen.horselive.databinding.ActivityRedPackageDetailsBinding;
import com.shenzhen.horselive.network.SchedulersCompat;
import com.shenzhen.horselive.presenter.LoadingViewObserver;

public class RedPackageDetailsActivity extends AppBarActivity {

    ActivityRedPackageDetailsBinding  binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityRedPackageDetailsBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setStyleDark();
        setTitle("领取详情");
        initViews();
        initData(0);
    }

    private void initData(int  page) {
        //统计
        ApiFactory.getApiService().redDetailsAbout(User.get().getMyInfo().getToken()
        ,getIntent().getStringExtra("id")
        ).compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new LoadingViewObserver<RedDetailsAboutBean>(this) {
                    @Override
                    protected void onNextInActive(RedDetailsAboutBean redDetailsAboutBean) {
                        if (redDetailsAboutBean.isSuccess())
                        {
                            binding.tvAbout.setText("已领取"+redDetailsAboutBean.getData().getReceiveQuantity()+"/"
                                    +redDetailsAboutBean.getData().getQuantity()+"个，共"+redDetailsAboutBean.getData().getReceiveAmount()+"元（服务费"
                                    +redDetailsAboutBean.getData().getServiceFee()+"）");
                        }else
                        {
                            showToast(redDetailsAboutBean.getErrMessage());
                        }
                    }
                });

        //获取领取人列表
        ApiFactory.getApiService().redDetailsListPeople(User.get().getMyInfo().getToken(),
                getIntent().getStringExtra("id"),page+""
                ).compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new LoadingViewObserver<RedDetailsBean>(this) {
                    @Override
                    protected void onNextInActive(RedDetailsBean redDetailsBean) {
                        if (binding.smartRefresh.isRefreshing())
                        {
                            binding.smartRefresh.finishRefresh();
                        }
                        if (binding.smartRefresh.isLoading())
                        {
                            binding.smartRefresh.finishLoadMore();
                        }
                        if (redDetailsBean.isSuccess())
                        {
                            setTitle(String.format("%s人领取",redDetailsBean.getTotal()));
                            if (page==0)
                            {
                            adapter.setNewInstance(redDetailsBean.getData());
                            }
                            else
                            {
                                if (redDetailsBean.getData()==null||redDetailsBean.getData().size()==0)
                                {
                                    binding.smartRefresh.finishLoadMoreWithNoMoreData();
                                }
                                else
                                {
                                    adapter.addData(redDetailsBean.getData());
                                }
                            }
                        }else
                        {
                            showToast(redDetailsBean.getErrMessage());
                        }
                    }
                });
    }

    RedDetailsAdapter  adapter;
    private int  nowPage = 0;
    private void initViews() {
      binding.recyclerview.setLayoutManager(new LinearLayoutManager(this));
        adapter = new RedDetailsAdapter(R.layout.item_red_details);
        binding.recyclerview.setAdapter(adapter);
        binding.smartRefresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                initData(++nowPage);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                     nowPage=0;
                    initData(nowPage);
            }
        });
    }

   public static void start(Context context,String  id) {
        Intent starter = new Intent(context, RedPackageDetailsActivity.class);
        starter.putExtra("id",id);
        context.startActivity(starter);
    }
}